function [QValuesSaveTable, AbsorptionSave, PhiSave] = Spara2QAR_AllInOne(runIDs, ParameterRange, FrequencySelectRange, PolarizedDirection, FileTag, SaveFlag, WhichPeak)
    %Spara2QAR_AllInOne 从当前打开的CST窗口，获取S参数，计算Q值和吸收值，保存在CST所在文件位置
    %@since 2022.4.22
    %@update: 汇总所有需要和不需要的值 把所有函数合并成这一个
    % 2022-07-27 21:42:42 控制统一点数
    %    *参数说明*
    %       @param  [runIDs] runID向量.
    %       @param  [ParameterRange] 对应的参数值变化范围.
    %       @param  [FrequencySelectRange] 需要计算Q值的范围.
    %       @param  [PolarizedDirection] 极化方向.
    %       @param  [FileTag] 保存文件名中加入的标志.
    %       @param  [SaveFlag] 是否保存文件，保存则不为0.
    %       @param  [WhichPeak] 选择哪一个峰，all,-1,1,2...
    %       @return [QValuesSave] 共振峰的频率，半高宽度，吸收率，反射率，总Q，Qr，Qd,runID,参数变化范围
    %       @return [AbsorptionSave] 频率与各个参数对应的吸收值.
    %       @return [PhiSave] 频率与主模态的相位值.
    %
    %    *使用说明*
    %       无参数调用， [~,~,~] = Spara2QAR_AllInOne()
    %       将按runID返回所有值，Q值默认计算第一个峰，默认频率范围请查看，极化方向为Y，不保存文件
    %
    %       [parameters,Abp_contour,Phi]=Spara2QAR_AllInOne(1:31,-85:5:60,[1.1,1.3],'Y','Full_Dx',0,-1)
    % ———————————————— POWERED BY YINWEI ————————————————

    %     close all;
    CST_Post = CST_MicrowaveStudio();

    % 默认参数
    if nargin == 0
        runIDs = CST_Post.getRunIDs;
        FrequencySelectRange = [1.18, 1.28];
        PolarizedDirection = 'Y';
        FileTag = [];
        SaveFlag = 0;
        WhichPeak = -1;
    end

    if upper(PolarizedDirection) == 'Y'
        DeltaS = 0;
    elseif upper(PolarizedDirection) == 'X'
        DeltaS = 2;
    end

    QValuesSave = [];
    AbsorptionSave = [];
    PhiSave = [];

    for eachepoch = 1:length(runIDs)

        everyrun = runIDs(eachepoch);
        [freq, sparam, ~] = CST_Post.getSParams(everyrun);

        % -----------------------
        % 进行插值，统一长度
        % FRE = linspace(freq(1), freq(end), 1001)';

        % for eachSparm = 1:size(sparam, 2)

        %     sparamNEW(:, eachSparm) = interp1(freq, sparam(:, eachSparm), linspace(FRE(1), FRE(end), length(FRE))');
        % end

        % sparam = sparamNEW;
        % freq = FRE;
        % -----------------------

        [ObtainedResults, Output_Abp, Phi] = ObtainPeakParameters(sparam(:, 1 + DeltaS:2 + DeltaS), freq, FrequencySelectRange);

        Feedback = ReserveWhichPeakParameters(ObtainedResults, WhichPeak, everyrun, ParameterRange(eachepoch));

        QValuesSave = [QValuesSave; Feedback];
        AbsorptionSave = [AbsorptionSave, Output_Abp];
        PhiSave = [PhiSave, Phi];
        fprintf(' %+12s \t %+12s \t %+12s \t %+12s \t %+12s \t %+12s %+12s \t %+12s \t %+12s \n', ["f0", 'FWHM', 'A0', 'R0', 'total Q', 'radative Q', 'disspiative Q', 'runID', 'DeltaX']);

        fprintf('%f \t %f \t %f \t %f \t %f \t %f \t %f \t %d \t %f\n', Feedback');
    end

    AbsorptionSave = [freq, AbsorptionSave];
    PhiSave = [freq, PhiSave];
    QValuesSaveTable = array2table(QValuesSave, 'variablenames', ...
        {'f0', 'FWHM', 'A0', 'R0', 'total Q', 'radative Q', 'disspiative Q', 'runID', 'DeltaX'});

    %-----------------

    HF = YW_Plot_Fast({QValuesSave(:, 9), QValuesSave(:, 5), QValuesSave(:, 9), QValuesSave(:, 6), QValuesSave(:, 9), QValuesSave(:, 7)}, {"r*-", "g:", "k--"}, ["Q", "Qr", "Qd"], ["parameters", 'Amplitude']);
    % plot(1:length(runIDs), QValuesSave(:, 1), 'r*-', 'linewidth', 2);
    % hold on;
    % plot(1:length(runIDs), QValuesSave(:, 3), 'b--', 'linewidth', 2);
    % plot(1:length(runIDs), QValuesSave(:, 4), 'g:', 'linewidth', 2);
    % legend('Q', 'P', 'Q*P');
    % %     xlim([runIDs(1) runIDs(end)]);
    % set(gca, 'xtick', 1:length(runIDs));
    % set(gca, 'XTickLabel', ParameterRange);
    figure;
    contourf(AbsorptionSave(:, 2:end));
    %-----------------
    if SaveFlag
        SaveFile([AbsorptionSave], [CST_Post.folder, '\Fre_Absorption_', FileTag, ], 'txt');
        SaveFile([QValuesSave, ParameterRange'], [CST_Post.folder, '\Q_', FileTag], 'txt');
        disp(['文件保存成功！'])
    end

    function [Results, Absorption, Phi] = ObtainPeakParameters(sparam, freq, FrequencySelectRange)
        %OBTAINPEAKPARAMETERS 第一个参数依次返回 共振峰的频率，半高宽度，吸收率，反射率，总Q，Qr，Qd
        Absorption = 1 - abs(sparam(:, 1)) .^ 2 - abs(sparam(:, 2)) .^ 2;
        %         Absorption = abs(sparam(:, 2)) .^ 2; % 需要获取透射率，临时操作，完成后恢复注释
        % Reflection = abs(sparam(:, 1)).^2 + abs(sparam(:, 2)).^2;
        Phi = angle(sparam(:, 1));

        IndicsRange = find(FrequencySelectRange(1) <= freq & FrequencySelectRange(2) >= freq);
        SeperatedPeak = Absorption(IndicsRange);
        SeperatedFreq = freq(IndicsRange);
        clearvars sparm freq IndicsRange

        try
            [pks, locs, width, ~] = findpeaks(SeperatedPeak, SeperatedFreq, 'widthreference', 'halfheight', 'MinPeakProminence', 0.0, 'MinPeakHeight', 0.0); % 结果都是一列一列的

            Reflection = 1 - pks;
            Q_Value = locs ./ width;
            % 默认以过阻尼的情况计算Q值，过临界耦合之后，需要手动调换
            QRadativeValue = 2 * Q_Value ./ (1 - sqrt(Reflection));
            QDisspiativeValue = 2 * Q_Value ./ (1 + sqrt(Reflection));
            Q_Value(1); % 看一下是否为空
            % 不为空则继续执行
        catch ME % 为空则跳转到这里

            if (strcmp(ME.identifier, 'MATLAB:badsubscript'))
                fprintf('*\n*\n*\n索引超出范围，具体请查看峰的形状（已经将Q值取为INf)*\n*\n*\n')
                Reflection = NaN;
                Q_Value = Inf;
                QRadativeValue = Inf;
                QDisspiativeValue = Inf;
                pks = NaN; locs = NaN; width = NaN;

            end

        end

        Results = [locs, width, pks, Reflection, Q_Value, QRadativeValue, QDisspiativeValue];
    end

    function Feedback = ReserveWhichPeakParameters(ImportedData, WhichPeak, everyrun, Deltax)
        %RESERVEWHICHPEAKPARAMETERS 选择要保存哪一个峰的信息
        if lower(WhichPeak) == 'all'
            % Qvalue = pParameters.Qvalue;
            % locs = pParameters.locs;
            % Feedback = [ImportedData, repmat([everyrun, Deltax], size(ImportedData, 1), 1)];
            disp(['已经取消一次性获取所有峰的信息功能，请逐个峰测试']);
            return;
        else

            try

                if WhichPeak == -1
                    Feedback = [ImportedData(end, :), everyrun, Deltax];
                else
                    Feedback = [ImportedData(WhichPeak, :), everyrun, Deltax];
                end

            catch ME

                if (strcmp(ME.identifier, 'MATLAB:badsubscript'))
                    fprintf('选择峰的位置与实际拥有的峰不匹配');
                    Feedback = [repmat(-1, 1, size(ImportedData, 2)), everyrun, Deltax];
                end

            end

        end

    end

end
